source("~/evo-dispersal/SRE/Stable range functions1D.R")
#setwd("~/Dropbox/Papers/Current/SRE/1D")

args=(commandArgs(TRUE))

#evaluate the arguments
# input arguments will be beta fID, ngens.init and ngens.final
for(i in 1:length(args)) {
	 eval(parse(text=args[[i]]))
}


maxD<-20
mutn<-0.05
K<-10
lmda<-2
#beta<--0.01
bf<-100
n<-K*bf
spX<-2*bf-1/beta
#ngens.init<-500
#ngens.final<-3000

# runs the thing over n generations

  pop<-init.inds(n, spX, maxD)
  for(ii in 1:ngens.init){
	  pop<-repro.disp(popmatrix=pop, spX=spX/2, K=K, lambda=lmda, beta=0, mutn=mutn, bf=spX/2, maxD=maxD, dvec=NULL)
  }
  dvec<-pop[,"D"]
  #plotter.mean(popmatrix=pop, spX=spX, K=K, beta=0, bf=spX/2, filename="SRE/1D/init.pdf")
  
  for(ii in 1:ngens.final){
    pop<-repro.disp(popmatrix=pop, spX=spX, K=K, lambda=lmda, beta=beta, mutn=mutn, bf=spX/2+0.5*(1/beta), maxD=maxD, dvec=dvec)
    if (max(pop[,"X"])>(spX-1)) break
  }
  #plotter.mean(popmatrix=pop, spX=spX, K=K, beta=beta, bf=spX/2+0.5*(1/beta), filename="SRE/1D/final.pdf")
  
  out<-list(parameters=list(ngens=ii, maxD=maxD, K=K, lambda=lmda, beta=beta, bf=bf, n=n, spX=spX, ngens.init=ngens.init,
  	ngens.final=ngens.final, mutn=mutn), population=pop)
  	
  save(out, file=paste("/homes/jc227089/SRE/oneD/NoDispEvol/NoDispEvol", fID, ".RData", sep=""))
	

